package tests;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;

public class PrintTextInJFrame
{
	private JFrame frameText;
	private JTextPane writtenArea;
	private String textOfWrittenArea;
	
	public PrintTextInJFrame()
	{
		
	}
	
	public PrintTextInJFrame(String title)
	{
		frameText = new JFrame();
		frameText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frameText.setTitle(title);
		JPanel panelTextArea= new JPanel();
		writtenArea = new JTextPane(new DefaultStyledDocument());
		writtenArea.setContentType("text/html");
		writtenArea.setEditable(true);
		JScrollPane scroll = new JScrollPane(writtenArea);
		scroll.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setPreferredSize(new Dimension(500, 500));
		scroll.setMinimumSize(new Dimension(500, 500));
		panelTextArea.add(scroll);
		frameText.add(panelTextArea);
		frameText.pack();
		frameText.setVisible(true);
		
	}
	
	public synchronized void write(String toWrite)
	{
		if(textOfWrittenArea == null)
		{
			textOfWrittenArea = "<p>" + toWrite + "</p>";
		}
		else
		{
			textOfWrittenArea = textOfWrittenArea + "<p>" + toWrite + "</p>";
		}
		
		writtenArea.setText(textOfWrittenArea);
	}
	
	

}
